package com.hjq.demo.ui.adapter

import android.content.Context
import android.net.Uri
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import cn.cucsi.myframe.R
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.dragswipe.listener.DragAndSwipeDataCallback
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.utils.DateUtils

/**
 * @author : 20720
 * @time   : 2024/5/27
 * desc    : 多媒体案例 顶部选择文件展示 列表适配器
 */
class MultimediaGridAdapter : BaseQuickAdapter<LocalMedia, QuickViewHolder>(),
    DragAndSwipeDataCallback {

    companion object {
        const val TYPE_CAMERA: Int = 1
        const val TYPE_PICTURE: Int = 2
    }

    override fun onCreateViewHolder(
        context: Context, parent: ViewGroup, viewType: Int
    ): QuickViewHolder {
        // 返回一个 ViewHolder
        return QuickViewHolder(R.layout.multimedia_filter_image, parent)
    }


    override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: LocalMedia?) {

        var mImg: ImageView = holder.getView(R.id.fiv)
        var mIvDel: ImageView = holder.getView(R.id.iv_del)
        var tvDuration: TextView = holder.getView(R.id.tv_duration)

        //少于MaxSize张，显示继续添加的图标
        if (getItemViewType(position) == TYPE_CAMERA) {
            mImg.setImageResource(R.drawable.multimedia_add_image_ic)
            mIvDel.visibility = View.INVISIBLE
        } else {
            mIvDel.visibility = View.VISIBLE
            val chooseModel = item?.chooseModel
//            val path = item?.availablePath
            val duration = item?.duration
            tvDuration.visibility =
                if (PictureMimeType.isHasVideo(item?.mimeType)) View.VISIBLE else View.GONE
            if (chooseModel == SelectMimeType.ofAudio()) {
                tvDuration.visibility = View.VISIBLE
                tvDuration.setCompoundDrawablesRelativeWithIntrinsicBounds(
                    R.drawable.ps_ic_audio, 0, 0, 0
                )
            } else {
                tvDuration.setCompoundDrawablesRelativeWithIntrinsicBounds(
                    R.drawable.ps_ic_video, 0, 0, 0
                )
            }
            tvDuration.text = DateUtils.formatDurationTime(duration!!)
            if (chooseModel == SelectMimeType.ofAudio()) {
                mImg.setImageResource(R.drawable.ps_audio_placeholder)
            } else {
                Glide.with(context).load(
                        if (item.isCut) {
                            item.cutPath // 裁剪后的图片地址
                        } else (if (item.isCompressed) {
                            item.compressPath // 压缩后的图片地址
                        } else if (PictureMimeType.isContent(item.availablePath)) {
                            Uri.parse(
                                item.availablePath // 需要解析的图片地址
                            )
                        } else {
                            item.availablePath // 实际存储位置
                        })
                    ).centerCrop().placeholder(R.color.white90)
                    .diskCacheStrategy(DiskCacheStrategy.ALL).into(mImg)
            }
        }
    }

    override fun dataMove(fromPosition: Int, toPosition: Int) {
        move(fromPosition, toPosition)
    }

    override fun dataRemoveAt(position: Int) {
        removeAt(position)
    }
}